//封装鉴权路由组件

import history from '@/utils/history'
import { hasToken } from '@/utils/storage'
import React from 'react'
import { Route, Redirect } from 'react-router-dom'

type Props = {
  path: string
  component: React.FC
}

export default function AuthRoute({ path, component: Component }: Props) {
  return (
    <Route
      path={path}
      render={() => {
        if (hasToken()) {
          // 组件一般大写，所以重命名了下
          return <Component />
        } else {
          // history第一个参数是url 第二个参数就是state
          return <Redirect to={{ pathname: '/login', state: { from: history.location.pathname } }} />
        }
      }}
    />
  )
}
